<?php

namespace app\store\model;

use think\Model;

class UserMonthCardModel extends Model
{
    protected $connection = 'db_platform_log';

    protected $table = 'user_score_log';

    const MONTH_CARD_ID = 5001;//月卡ID
    const MONTH_CARD_TYPE = '领取月卡';//月卡领取

    // 搜索的时间
    protected $date_range = null;

    // 搜索的用户ids(注册或者登录)子查询语句
    protected $user_sub_sql = null;

    // 筛选的用户字段和值
    protected $user_field = null;
    protected $user_value_start = null;
    protected $user_value_end = null;

    /**
     * UserMonthCardModel constructor.
     * @param array $data
     * @throws \think\exception\DbException
     */
    public function __construct($data = [])
    {
        parent::__construct($data);
        $time_field = request()->get('time_field');
        $time_value = request()->get('time_value');

        $this->user_field = request()->get('user_field');
        $this->user_value_start = request()->get('user_value_start');
        $this->user_value_end = request()->get('user_value_end');

        // 获取具体时间 单天
        if (!empty($time_field) and !empty($time_value)) {
            $this->date_range = [$time_value, $time_value . ' 23:59:59'];
        } else {
            $this->date_range = [date('Y-m-d'), date('Y-m-d 23:59:59')];
        }

        // 根据获取筛选的时间类型（是注册还是登录）获取对应的用户
        if (empty($time_field) or $time_field == 'RegisterDate') {
            $this->user_sub_sql = UserRegModel::getUserIdsSubSql($this->date_range);
        } else {
            $this->user_sub_sql = UserLoginModel::getUserIdsSubSql($this->date_range);
        }
    }


    /**获取月卡
     * @param $where
     * @return array
     */
    public function getMonthCard($where)
    {
        // 如果筛选了用户条件
        if (!is_null($this->user_value_start) and !is_null($this->user_value_end)) {
            // 炮倍筛选
            if ($this->user_field == 'cannon') {

                // 购买人数
                $query = TradeModel::alias('t')
                    ->where($where)
                    ->where('t.goods_id', self::MONTH_CARD_ID)
                    ->where('t.status', TradeModel::PAY_PAID)
                    ->where('t.paydate', 'between', $this->date_range)
                    ->group('t.UserID')
                    ->leftJoin('qpaccountsdb.user_game_info g', 't.UserID = g.UserID')
                    ->where('t.UserID in '.$this->user_sub_sql);

                //激活人数
                $query2 = self::where($where)
                    ->alias('l')
                    ->where('l.type', self::MONTH_CARD_TYPE)
                    ->group('l.UserID')
                    ->where('l.RecordTime', 'between', $this->date_range)
                    ->leftJoin('qpaccountsdb.user_game_info g', 'l.UserID = g.UserID')
                    ->where('l.UserID in '.$this->user_sub_sql);
            } else {
                // 购买人数
                $query = TradeModel::alias('t')
                    ->group('t.UserID')
                    ->leftJoin('qpaccountsdb.accounts_info a', 't.UserID = a.UserID')

                    ->where($where)
                    ->where('t.goods_id', self::MONTH_CARD_ID)
                    ->where('t.status', TradeModel::PAY_PAID)
                    ->where('t.paydate', 'between', $this->date_range)
                    ->where('t.UserID in '.$this->user_sub_sql);

                //激活人数
                $query2 = self::where($where)
                    ->group('l.UserID')
                    ->alias('l')
                    ->leftJoin('qpaccountsdb.accounts_info a', 'l.UserID = a.UserID')

                    ->where('l.type', self::MONTH_CARD_TYPE)
                    ->where('l.RecordTime', 'between', $this->date_range)
                    ->where('l.UserID in '.$this->user_sub_sql);
            }
        } else {
            // 购买人数
            $query = TradeModel::alias('t')
                ->group('t.UserID')

                ->where('t.goods_id', self::MONTH_CARD_ID)
                ->where('t.status', TradeModel::PAY_PAID)
                ->where('t.paydate', 'between', $this->date_range)
                ->where('t.UserID in '.$this->user_sub_sql);

            //激活人数
            $query2 = self::alias('l')
                ->group('l.UserID')

                ->where('l.type', self::MONTH_CARD_TYPE)
                ->where('l.RecordTime', 'between', $this->date_range)
                ->where('l.UserID in '.$this->user_sub_sql);
        }

        $number = $query->count();
        $times = $query2->count();
        // echo $query->getLastSql();
        // echo $query2->getLastSql();

        return [
            'pay' => $number,
            'active' => $times
        ];
    }

}